package com.xfcy.blog.controller.system;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xfcy.blog.annotation.OperationLogger;
import com.xfcy.blog.common.R;
import com.xfcy.blog.dto.JobDTO;
import com.xfcy.blog.entity.Job;
import com.xfcy.blog.exception.TaskException;
import com.xfcy.blog.service.JobService;
import com.xfcy.blog.valid.AddGroup;
import com.xfcy.blog.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度表(Job)表控制层
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:06
 */
@RestController
@RequestMapping("system/job")
@Api(tags = "定时任务管理")
public class JobController {

    @Resource
    private JobService jobService;

    @GetMapping("/list")
    @ApiOperation(value = "定时任务列表", httpMethod = "GET", response = R.class, notes = "定时任务列表")
    @SaCheckLogin
    public R list(Long pageNo, Long pageSize, String jobName, String jobGroup, String status) {
        return jobService.listJob(pageNo, pageSize, jobName, jobGroup, status);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除定时任务", httpMethod = "DELETE", response = R.class, notes = "删除定时任务")
    @SaCheckPermission("/system/job/delete")
    @OperationLogger(value = "删除定时任务")
    public R delete(Long jobId) throws SchedulerException {
        return jobService.deleteJob(jobId);
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "删除定时任务", httpMethod = "DELETE", response = R.class, notes = "删除定时任务")
    @SaCheckPermission("/system/job/delete")
    public R deleteBatch(List<Long> ids) {
        return jobService.deleteBatchJob(ids);
    }

    @GetMapping("/info")
    @ApiOperation(value = "定时任务详情", httpMethod = "GET", response = R.class, notes = "定时任务详情")
    @SaCheckPermission("/system/job/info")
    @OperationLogger(value = "定时任务详情")
    public R info(Long jobId) {
        return jobService.getJobById(jobId);
    }

    @PostMapping("/add")
    @SaCheckPermission("/system/job/add")
    @ApiOperation(value = "添加定时任务", httpMethod = "POST", response = R.class, notes = "添加定时任务")
    @OperationLogger(value = "添加定时任务")
    public R add(@Validated(AddGroup.class) @RequestBody JobDTO jobDTO) throws SchedulerException, TaskException {
        return jobService.addJob(jobDTO);
    }

    @PostMapping("/update")
    @SaCheckPermission("/system/job/update")
    @ApiOperation(value = "修改定时任务", httpMethod = "POST", response = R.class, notes = "修改定时任务")
    @OperationLogger(value = "修改定时任务")
    public R update(@Validated(UpdateGroup.class) @RequestBody JobDTO jobDTO) throws SchedulerException, TaskException {
        return jobService.updateJob(jobDTO);
    }

    @PostMapping("/change")
    @SaCheckPermission("/system/job/change")
    @ApiOperation(value = "修改状态", httpMethod = "POST", response = R.class, notes = "修改状态")
    @OperationLogger(value = "修改状态")
    public R changeStatus(@RequestBody JobDTO jobDTO) throws SchedulerException {
        return jobService.changeStatus(jobDTO);
    }

    @PostMapping("/run")
    @SaCheckPermission("/system/job/run")
    @ApiOperation(value = "立即执行", httpMethod = "POST", response = R.class, notes = "立即执行")
    @OperationLogger(value = "立即执行")
    public R run(@RequestBody Job job) {
        return jobService.run(job);
    }

}

